package com.raymond.cmpp.client.codec.decoding;

import com.raymond.cmpp.common.codec.annotation.MsgCodec;
import com.raymond.cmpp.common.codec.decoding.BaseDecoder;
import com.raymond.cmpp.common.constance.GlobalConstance;
import com.raymond.cmpp.common.enums.CommandIdType;
import com.raymond.cmpp.common.msg.BaseAgreement;
import com.raymond.cmpp.common.msg.response.ConnectResponseMessage;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.ReferenceCountUtil;

import java.util.List;

/**
 * 连接响应解码
 *
 * @author :  raymond
 * @version :  V1.0
 * @date :  2021-08-05 15:31
 */
@MsgCodec(commandId = CommandIdType.CONNECT_RESPONSE)
public class ConnectResponseMessageDecoder extends BaseDecoder {


    @Override
    public void decode(ChannelHandlerContext ctx, BaseAgreement msg, List<Object> out) throws Exception {
        ByteBuf bodyBuffer = Unpooled.wrappedBuffer(msg.getBodyBuffer());
        ConnectResponseMessage responseMessage = new ConnectResponseMessage(msg.getSequenceId());
        responseMessage.setStatus(bodyBuffer.readUnsignedInt());
        responseMessage.setAuthenticatorIsmg(bodyBuffer.readCharSequence(16, GlobalConstance.DEFAULT_TRANSPORT_CHARSET).toString().trim());
        responseMessage.setVersion(bodyBuffer.readUnsignedByte());
        ReferenceCountUtil.release(bodyBuffer);
        out.add(responseMessage);
    }
}
